package com.hducms.cms.action.admin.assist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.aspectj.weaver.NewConstructorTypeMunger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hducms.cms.entity.main.Channel;
import com.hducms.cms.entity.main.CmsChecked;
import com.hducms.cms.entity.main.CmsUser;
import com.hducms.cms.entity.main.CmsUserExt;
import com.hducms.cms.entity.main.Content;
import com.hducms.cms.entity.main.ContentExt;
import com.hducms.cms.service.ChannelService;
import com.hducms.cms.service.CmsCheckedService;
import com.hducms.cms.service.CmsUserExtService;
import com.hducms.cms.service.CmsUserService;
import com.hducms.cms.service.ContentExtService;
import com.hducms.cms.service.ContentService;

@Controller
public class CmsCheckedAct {

	@RequestMapping(value="/content/check_list.do")
	public String checklist(Integer id,ModelMap model) {
		List<CmsChecked> cmsCheckeds = manager.getChecklistByContentId(id);
		String title = contentService.findById(id).getContentExt().getTitle();
		int channelId = contentService.findById(id).getChannelId();
		Set<CmsUser> checkedUserList = new HashSet<CmsUser>();
//		Map<Integer, Object> allUserMap = new HashMap<Integer, Object>();//
		Set<CmsUser> allUserList = new HashSet<CmsUser>();
		Integer[] groups = channelService.getViewGroupIds(channelId);
		for (Integer integer : groups) {
			List<CmsUser> userList = cmsUserService.findByGroupId(integer);
			allUserList.addAll(userList);
		}
		for (CmsChecked cmsChecked : cmsCheckeds) {
			CmsUser temp = cmsUserService.findById(cmsChecked.getUserId());
			CmsUserExt tempE = userEservice.findById(cmsChecked.getUserId());
			if (temp != null) {
				cmsChecked.setUser(temp);
				if (tempE.getRealname()== null || tempE.getRealname().length()<1) {
					cmsChecked.setRealname("未填姓名");
				}
				else {
					cmsChecked.setRealname(tempE.getRealname());
				}
				checkedUserList.add(temp);
			}
		}
		for (CmsUser cmsUser : checkedUserList) {
			allUserList.remove(cmsUser);
		}
		List<CmsUser> list1 = new ArrayList<CmsUser>(allUserList);
		for (CmsUser cmsUser : list1) {
			CmsUserExt tempE = userEservice.findById(cmsUser.getUserId());
			if (tempE.getRealname()==null || tempE.getRealname().length()<1) {
				tempE.setRealname("未填姓名");
			}
			if (tempE.getMobile()==null || tempE.getMobile().length()<1) {
				tempE.setMobile("未填手机号");
			}
			cmsUser.setUserExt(tempE);
		}
		model.addAttribute("checked",cmsCheckeds);
		model.addAttribute("unChecked", list1);
		model.addAttribute("title",title);
		return "content/check_list";
	}
	
	@Autowired
	private CmsCheckedService manager;
	@Autowired
	private ContentService contentService;
	@Autowired
	private ChannelService channelService;
	@Autowired
	private CmsUserService cmsUserService;
	@Autowired
	private CmsUserExtService userEservice;
}
